using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_LocationManagementl : admin_BaseUserControl
{
    LocationDAO daoLocation = new LocationDAO();
    Location loc = new Location();
    int id = 0;
    public string GetImageURL()
    {
        return loc.ImageURL;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "location";
        if (Request["mode"] == "1")
        {
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            try
            {
                id = int.Parse(Request["id"]);
                CurrentID = Request["id"];
                loc = daoLocation.GetLocation(id);
                txtName.Text = loc.Name;
                txtDescription.Text = loc.Description;
                chkState.Checked = loc.State;
                txtTag.Text = loc.Tag;
                fckArticle.Value = loc.Article;
            }
            catch (Exception ex)
            {
                ShowMessage("Error occure when load location information");
            }
        }
        else
        {
            txtName.Text = "";
            txtDescription.Text = "";
            txtTag.Text = "";
            chkState.Checked = false;

            lnkAdd.Visible = true;
            lnkUpdate.Visible = false;
            lnkDelete.Visible = false;
        }
    }
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        
        loc = new Location();
        loc.Name = txtName.Text;
        loc.Description = txtDescription.Text;
        loc.Tag = txtTag.Text;
        loc.State = chkState.Checked;
        loc.Article = fckArticle.Value;
        loc.ImageURL=Request["txtImage"];
        loc.ModifiedDate = DateTime.Now;
        loc.ModifiedBy = 1;
        if (daoLocation.Insert(loc) > 0)
        {
            ShowMessage("New location is created");            
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            CurrentID = loc.Id.ToString();
        }
        else ShowMessage("Error occured when trying to create new location. Action failed");
       // SetAccordionPane(typeof(admin_BaseUserControl), 4);
    }
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        if (id == 0)
        {
            try
            {
                id = int.Parse(CurrentID);
            }
            catch (Exception ex)
            {
                ShowMessage("Error occured when trying to update location information");
            }
        }
        loc = new Location();
        loc.Id = id;
        loc.Name = txtName.Text;
        loc.Description = txtDescription.Text;
        loc.Article = fckArticle.Value;
        loc.Tag = txtTag.Text;
        loc.State = chkState.Checked;
        loc.ImageURL = Request["txtImage"];
        loc.ModifiedDate = DateTime.Now;
        loc.ModifiedBy = 1;
        if (daoLocation.Update(loc) > 0)
        {
            ShowMessage("Information is updated");           
        }
        else ShowMessage("Error occured when trying to create new location. Action failed");
        lnkAdd.Visible = false;
        lnkUpdate.Visible = true;
        lnkDelete.Visible = true;
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        if (id == 0)
        {
            try
            {
                id = int.Parse(CurrentID);
            }
            catch (Exception ex)
            {
                ShowMessage("Error occured when trying to delete location information");
            }
        }
        if (daoLocation.Delete(id) > 0)
        {
            ShowMessage("Location information is deleted");
            lnkDelete.Visible = false;
            lnkUpdate.Visible = false;
            lnkAdd.Visible = true;
        }
        else ShowMessage("Error occured when trying to delete location information");
    }
}
